package dm.ast;


import test.envset.GlobalState;

/**
 * selecct语句中的limit子句
 */
public class DmRowLimit extends DmASTNode{
    //ROWLIMIT子句的3种格式
    public static int SUPPORT_FORMAT = 3;  //0:OFFSET子句 1: FETCH子句 2: OFFSET子句 FETCH子句
    //总共有5个选项
    public static int FLAGS_CNT = 5;  //OFFSET ... ROW|ROWS FIRST|NEXT PERCENT FETCH ... ROW|ROWS ONLY | WITH TIES

    private static int OFFSET_ROW_FLAG = 1;
    private static int FETCH_FIRST_FLAG = 1 << 1;
    private static int FETCH_PERCENT_FLAG = 1 << 2;
    private static int FETCH_ROW_FLAG = 1 << 3;
    private static int FETCH_ONLY_FLAG = 1 << 4;

    //由于这里都可以为字符串NULL，所以这里定义为字符串
    private String offsetNum = null;  //偏移量
    private String fetchNum = null;  //fetch的数目
    private int formatIndex = -1;  //实际使用的格式
    private int choicesFlag = 0;

    public void setOffsetNum(String offset){
        this.offsetNum = offset;
    }

    public void setFetchNum(String fetch) {
        this.fetchNum = fetch;
    }

    public void setFormat(int index) {
        this.formatIndex = index;
    }

    public void setFlags(int flags) {
        this.choicesFlag = flags;
    }

    @Override
    public void fill(GlobalState globalState) {
        if(formatIndex == -1){
            formatIndex = 0;
            offsetNum = "1";
            fetchNum = "1";
        }
        isCompleted = true;
    }

    @Override
    public void setWidt() {
        //暂时不考虑复杂表达式
        wid_t = 2;
    }

    @Override
    public void setDept() {
        dep_t = 1;
    }

    @Override
    public DmDataType getExpressionType() {
        return null;
    }
    //处理符号，拼接
    @Override
    public String toSql(){
        StringBuilder sql = new StringBuilder();
        if(formatIndex == -1){
            sql.append(" " + this.getClass().getSimpleName());
        }else {
            if(formatIndex != 1){ //offset子句
                sql.append(String.format(" OFFSET %s" , offsetNum));
                if((choicesFlag & OFFSET_ROW_FLAG) == OFFSET_ROW_FLAG){  //判断OFFSET_ROW标志位是否为1
                    sql.append("ROW ");
                }else {
                    sql.append("ROWS ");
                }
            }
            if(formatIndex != 0){ //fetch子句
                sql.append(" FETCH ");
                if((choicesFlag & FETCH_FIRST_FLAG) == FETCH_FIRST_FLAG) {  //判断FETCH_FIRST标志位是否为1
                    sql.append("FIRST ");
                }else{
                    sql.append("NEXT ");
                }
                sql.append(String.format("%s " , fetchNum));
                if((choicesFlag & FETCH_PERCENT_FLAG) == FETCH_PERCENT_FLAG) {  //判断FETCH_PERCENT标志位是否为1
                    sql.append("PERCENT ");
                }
                if((choicesFlag & FETCH_ROW_FLAG) == FETCH_ROW_FLAG){  //判断FETCH_ROW标志位是否为1
                    sql.append("ROW ");
                }else {
                    sql.append("ROWS ");
                }
                if((choicesFlag & FETCH_ONLY_FLAG) == FETCH_ONLY_FLAG){  //判断FETCH_ONLY标志位是否为1
                    sql.append("ONLY ");
                }else {
                    sql.append("WITH TIES ");
                }
            }
        }
        return sql.toString();
    }
}
